/*
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

package net.dbtrans.web.contorller;

import java.util.List;

import org.apache.mybatis.jpa.persistence.JpaPageResults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.dbtrans.authn.annotation.CurrentUser;
import net.dbtrans.entity.DbTables;
import net.dbtrans.entity.Message;
import net.dbtrans.entity.UserInfo;
import net.dbtrans.persistence.service.DbTablesService;
import net.dbtrans.web.component.TreeAttributes;
import net.dbtrans.web.component.TreeNode;

@Controller
@RequestMapping(value={"/metadata/tables"})
public class DbTablesController {
	final static Logger _logger = LoggerFactory.getLogger(DbTablesController.class);
	
	@Autowired
	DbTablesService dbTablesService;
	
	@RequestMapping(value = { "/fetch" }, produces = {MediaType.APPLICATION_JSON_VALUE})
	@ResponseBody
	public ResponseEntity<?> fetch(DbTables dbTable,@CurrentUser UserInfo currentUser) {
		_logger.debug(""+dbTable);
		dbTable.setInstId(currentUser.getInstId());
		return new Message<JpaPageResults<DbTables>>(
				dbTablesService.queryPageResults(dbTable)).buildResponse();
	}
	
	@RequestMapping(value = { "/get/{id}" }, produces = {MediaType.APPLICATION_JSON_VALUE})
	public ResponseEntity<?> get(@PathVariable("id") String id) {
		DbTables dbTable = dbTablesService.get(id);

		return new Message<DbTables>(dbTable).buildResponse();
	}
	
	@ResponseBody
	@RequestMapping(value={"/add"}, produces = {MediaType.APPLICATION_JSON_VALUE})
	public ResponseEntity<?> insert(@RequestBody  DbTables dbTable,@CurrentUser UserInfo currentUser) {
		_logger.debug("-Add  :" + dbTable);
		dbTable.setInstId(currentUser.getInstId());

		if (dbTablesService.insert(dbTable)) {
			return new Message<DbTables>(Message.SUCCESS).buildResponse();
		} else {
			return new Message<DbTables>(Message.FAIL).buildResponse();
		}
	}
	
	@ResponseBody
	@RequestMapping(value={"/update"}, produces = {MediaType.APPLICATION_JSON_VALUE})
	public ResponseEntity<?> update(@RequestBody  DbTables dbTable,@CurrentUser UserInfo currentUser) {
		_logger.debug("-update  :" + dbTable);
		dbTable.setInstId(currentUser.getInstId());
		if (dbTablesService.update(dbTable)) {
		    return new Message<DbTables>(Message.SUCCESS).buildResponse();
		} else {
			return new Message<DbTables>(Message.FAIL).buildResponse();
		}
	}
	
	@ResponseBody
	@RequestMapping(value={"/delete"}, produces = {MediaType.APPLICATION_JSON_VALUE})
	public ResponseEntity<?> delete(@RequestParam("ids") String ids,@CurrentUser UserInfo currentUser) {
		_logger.debug("-delete  ids : {} " , ids);
		if (dbTablesService.deleteBatch(ids)) {
			 return new Message<DbTables>(Message.SUCCESS).buildResponse();
		} else {
			return new Message<DbTables>(Message.FAIL).buildResponse();
		}
	}
	
	@ResponseBody
	@RequestMapping(value={"/tree"}, produces = {MediaType.APPLICATION_JSON_VALUE})
	public ResponseEntity<?> tree(@ModelAttribute DbTables dbTable,@CurrentUser UserInfo currentUser) {
		_logger.debug("-query  {}" , dbTable);
		dbTable.setInstId(currentUser.getInstId());
		List<DbTables>  tableList = dbTablesService.query(dbTable);
		TreeAttributes treeAttributes = new TreeAttributes();
		TreeNode treeRootNode = new TreeNode(dbTable.getProjectId(),dbTable.getProjectName());
		treeRootNode.setExpanded(true);
		treeRootNode.setLeaf(false);
		treeAttributes.setRootNode(treeRootNode);
		
		for (DbTables table : tableList) {
			TreeNode treeNode = new TreeNode(table.getId(),table.getTableName()+"["+table.getComments()+"]");
			treeNode.setExpanded(false);
			treeNode.setLeaf(false);
			treeNode.setParentKey(dbTable.getProjectId());
			treeNode.setParentTitle(dbTable.getProjectName());
			treeAttributes.addNode(treeNode);
		}
		
		return new Message<TreeAttributes>(Message.SUCCESS,treeAttributes).buildResponse();
	}

}
